package com.lhkj.ct.meta.modules.report.word.personal;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.google.common.collect.ImmutableBiMap;
import com.lhkj.ct.base.utils.file.word.xwpf.CustomXWPFDocument;
import com.lhkj.ct.base.utils.file.word.xwpf.ExportWord;
import com.lhkj.ct.base.utils.file.word.xwpf.XWPFTableHelper;
import com.lhkj.ct.meta.modules.psychology.model.entity.TblScaleReportContent;
import com.lhkj.ct.meta.modules.psychology.model.vo.PersonalReportInfo;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.ByteArrayInputStream;
import java.util.Collection;
import java.util.Map;

/**
 * <p>
 * 描述
 * </p>
 *
 * @author job
 * @since 2023/10/22
 */
public abstract class BaseLayoutHandler<T> {

    private static final Logger log = LoggerFactory.getLogger(BaseLayoutHandler.class);

    CustomXWPFDocument document ;

    protected final ExportWord word = new ExportWord();

    public BaseLayoutHandler() {
        init();
    }

    protected abstract void init();

    protected void builderTitle(String title) {
        // step 1.报告标题
        word.replaceInPara(document.getParagraphArray(0), ImmutableBiMap.of("scaleName", title));
    }

    protected void builderBasicInfoTable(PersonalReportInfo.BasicInfo basicInfo) {
        Map<String, Object> map = BeanUtil.beanToMap(basicInfo);
        // step 1.报告标题
        word.replaceInTable(document.getTableArray(0), map);
    }


    protected void generateDimReportTable(XWPFTable table, Collection<TblScaleReportContent> bgInfo) {
        Font globalFont = new Font("宋体", Font.PLAIN, 9);
        XWPFTableHelper xwpfTableHelper = word.getXwpfTableHelper();
        CTTrPr trPr = table.getRow(0).getCtRow().getTrPr();
        CTHeight ht = trPr.getTrHeightArray(0);
        for (TblScaleReportContent entity : bgInfo) {
            XWPFTableRow row = table.createRow();
            xwpfTableHelper.setTableRowHeight(row, ht.getVal().intValue(), null);
            xwpfTableHelper.setTableRowStyle(row, null, XWPFTableCell.XWPFVertAlign.CENTER, STJc.LEFT);
            xwpfTableHelper.setTableCellFont(row.getCell(0), globalFont, entity.getDimName());
            xwpfTableHelper.setTableCellFont(row.getCell(1), globalFont, entity.getOriginalScore() == null ? "--" : String.valueOf(entity.getOriginalScore()));
            xwpfTableHelper.setTableCellFont(row.getCell(2), globalFont, entity.getRealScore() == null ? "--" : String.valueOf(entity.getRealScore()));
            xwpfTableHelper.setTableCellFont(row.getCell(3), globalFont, entity.getExStatus());
        }
    }

    /**
     * 签名
     * @param basicInfo     signature电子签名，如不存在则使用reviewBy文字签名
     * @param location      签名文档位置
     */
    protected void sign(PersonalReportInfo.BasicInfo basicInfo, XWPFRun location) {
        if (ArrayUtil.isNotEmpty(basicInfo.getSignature())) {
            try {
                document.createPicture(
                        document.addPictureData(new ByteArrayInputStream(basicInfo.getSignature()), XWPFDocument.PICTURE_TYPE_PNG),
                        1,
                        90,
                        43,
                        location);
            } catch (InvalidFormatException e) {
                log.error("电子签名读取失败，原因：{}", ExceptionUtils.getStackTrace(e));
            }
        }
//        else {
//            location.setText(basicInfo.getReviewBy());
//        }
    }

    public abstract XWPFDocument build(T data);
}
